/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.renderer
{
	import flash.system.System;
	
	import multimedia.model.LoginModel;
	import multimedia.model.vo.MediaVO;
	
	import mx.controls.treeClasses.TreeItemRenderer;
	import mx.controls.treeClasses.TreeListData;

	public class MediaTreeItemRenderer extends TreeItemRenderer
	{

		private var deleteButton:DeleteMediaButton;
		
		private var previewButton:PreviewMediaButton;
		
		private var editButton:EditMediaButton;
		
		private var changeApproveStatus:ChangeApprovedStatusButton;

		public function MediaTreeItemRenderer()
		{
			super();
			this.useHandCursor = true;
			this.buttonMode = true;
		}

		/**
		 * Override the set method for the data property
		 * to set the font color and style of each node.
		 */        
		override public function set data(value:Object):void
		{
			super.data = value;
			setStyle("color", 0x000000);
			setStyle("textRollOverColor", 0x000000);
			setStyle("textSelectedColor", 0x000000);
			setStyle("fontWeight", 'normal');
			//item is disabled because same user cannot approve it
			if (super.data && super.data is MediaVO && (super.data as MediaVO).addedBy.user_id == LoginModel.getInstance().user.user_id)
			{
				setStyle("color", 0xc4c4c4);
				setStyle("fontWeight", 'bold');
				setStyle("textRollOverColor", 0xc4c4c4);
				setStyle("textSelectedColor", 0xc4c4c4);
			}
			else if(super.data is MediaVO)
			{
				var media:MediaVO = super.data as MediaVO;
				
				if(media.valid_date - new Date().time / 1000 < 3 * 60 * 60 * 24)
				{
					setStyle("color", 0xee7600);
					setStyle("fontWeight", 'normal');	
					setStyle("textRollOverColor", 0xee7600);
					setStyle("textSelectedColor", 0xee7600);
				}
				if(media.valid_date - new Date().time / 1000 < 60 * 60 * 24)
				{
					setStyle("color", 0xff0000);
					setStyle("fontWeight", 'normal');	
					setStyle("textRollOverColor", 0xff0000);
					setStyle("textSelectedColor", 0xff0000);
				}
				if(media.start_date > new Date().time / 1000)
				{
					setStyle("color", 0x1f82b8);
					setStyle("fontWeight", 'normal');	
					setStyle("textRollOverColor", 0x1f82b8);
					setStyle("textSelectedColor", 0x1f82b8);
				}
			}
		} 

		// Override the updateDisplayList() method 
		// to set the text for each tree node.      
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			label.width = 310;
			if (super.data)
			{
				if (!(TreeListData(super.listData).item is MediaVO))
				{
					super.label.text = TreeListData(super.listData).label;
					if(previewButton != null)
					{
						previewButton.visible = false;
					}
					
					if(deleteButton != null)
					{
						deleteButton.visible = false;
					}
					if(editButton != null)
					{
						editButton.visible = false;
					}
					if(changeApproveStatus != null)
					{
						changeApproveStatus.visible = false;
					}
				}
				else
				{
					var media:MediaVO = TreeListData(super.listData).item as MediaVO;
					if (deleteButton == null)
					{
						deleteButton = new DeleteMediaButton();
						deleteButton.x = this.width - deleteButton.width - 5;
						addChild(deleteButton);
					} else
					{
						deleteButton.x = this.width - deleteButton.width - 5;
						
						deleteButton.visible = true;//;(media.addedBy.user_id != LoginModel.getInstance().user.user_id && media.approved) || !media.approved;
					}
					
					if (previewButton == null)
					{
						previewButton = new PreviewMediaButton();
						previewButton.x = this.width - previewButton.width - 16 - 10;
						addChild(previewButton);
					} else
					{
						previewButton.visible = true;
						previewButton.x = this.width - previewButton.width - 16 - 10;
					}
					
					if (changeApproveStatus == null)
					{
						changeApproveStatus = new ChangeApprovedStatusButton(media.approved);
						changeApproveStatus.x = this.width - changeApproveStatus.width - 32 - 15;
						addChild(changeApproveStatus);
					} else
					{
						changeApproveStatus.visible = true;
						changeApproveStatus.x  = this.width - changeApproveStatus.width - 32 - 15;
						changeApproveStatus.visible = media.addedBy.user_id != LoginModel.getInstance().user.user_id || media.approved || LoginModel.getInstance().user.user_group.group_id == 4
					}
					
					if (editButton == null)
					{
						editButton = new EditMediaButton();
						editButton.x = this.width - editButton.width - 32 - 15 - (changeApproveStatus.visible ? 22 : 0);
						addChild(editButton);
					} else
					{
						editButton.visible = true;
						editButton.x = this.width - editButton.width - 32 - 15 - (changeApproveStatus.visible ? 22 : 0);
						editButton.visible = !media.approved;
					}
					
					deleteButton.node = TreeListData(super.listData);
					previewButton.node = TreeListData(super.listData);
					editButton.node = TreeListData(super.listData);
					changeApproveStatus.node = TreeListData(super.listData);
				}
			}
		}
	}
}